package com.decent.manager.system.service.impl;

import com.decent.common.bo.MessageBean;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.oss.enums.OssFolderEnum;
import com.decent.common.oss.util.OssUtil;
import com.decent.manager.config.oss.OssConfiguration;
import com.decent.manager.system.dao.SystemBrandDao;
import com.decent.manager.system.dao.SystemClassifyDao;
import com.decent.manager.system.dto.SystemClassifyDTO;
import com.decent.manager.system.service.SystemClassifyService;
import com.decent.manager.system.vo.SystemClassifyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import reactor.util.function.Tuple2;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author: decent Gjh
 * @date: 2022/10/25/18:35
 * @description:
 */
@Service
@Slf4j
public class SystemClassifyServiceImpl implements SystemClassifyService {
    private final ApplicationContext applicationContext;
    private final SystemClassifyDao classifyDao;
    private final SystemBrandDao brandDao;

    public SystemClassifyServiceImpl(SystemClassifyDao classifyDao, SystemBrandDao brandDao, ApplicationContext applicationContext) {
        this.classifyDao = classifyDao;
        this.brandDao = brandDao;
        this.applicationContext = applicationContext;
    }

    /**
     * 新建类别
     * @param classifyDTO 类别信息
     * @return 是否成功
     */
    @Override
    public MessageBean insertClassify(SystemClassifyDTO classifyDTO) {
        // oss部分
        if (classifyDTO.getIcoUrl()==null||classifyDTO.getIcoUrl().isEmpty()){
            MultipartFile file = classifyDTO.getFile();
            String fileName = file.getOriginalFilename();
            String type = "";
            if (Objects.nonNull(fileName)){
                type = Objects.requireNonNull(file.getOriginalFilename()).substring(fileName.lastIndexOf("."));
            }
            Tuple2<String, String> logo = OssUtil.submitFile(file, OssFolderEnum.BANNER, UUID.randomUUID()+type, true);
            classifyDTO.setIcoUrl(logo.getT2());
        }
        classifyDao.insert(classifyDTO);
        return MessageBean.success();
    }

    /**
     * 删除分类
     * @param classifyDTO 分类信息
     * @return 是否成功
     */
    @Override
    public MessageBean deleteClassify(SystemClassifyDTO classifyDTO) {
        // 验证分类下是否还包含品牌
        if (brandDao.selectNumberByClassifyId(classifyDTO.getId())>0){
            return MessageBean.fail("删除失败,该分类下还有品牌存在");
        }
        // TODO oss文件删除需返回知否成功 成功才可进行下一步(需修改模板核心代码)
        SystemClassifyVO vo = classifyDao.selectClassifyById(classifyDTO.getId());
        if (vo == null) {
            return new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,"分类不存在");
        }
        String ossPath = vo.getIcoUrl().substring(
                applicationContext.getBean("OssConfig", OssConfiguration.class).getPublicEndPoint().length()+1);
        OssUtil.deleteFile(ossPath);
        int rowNum = classifyDao.deleteClassify(classifyDTO.getId());
        return rowNum == 0 ? MessageBean.fail("删除失败"):MessageBean.success();
    }

    /**
     * 更新分类
     * @param classifyDTO 分类信息
     * @return 是否成功
     */
    @Override
    public MessageBean updateClassify(SystemClassifyDTO classifyDTO) {
        int rowNum = classifyDao.updateClassify(classifyDTO);
        return rowNum == 0 ? MessageBean.fail("删除失败,指定数据不存在"):MessageBean.success();
    }

    /**
     * 查找分类
     * @param classifyDTO 分类信息
     * @return 是否成功
     */
    @Override
    public MessageBean selectClassify(SystemClassifyDTO classifyDTO) {
        List<SystemClassifyVO> classifies = classifyDao.selectClassify(classifyDTO);
        if (classifies==null||classifies.isEmpty()) {
            return MessageBean.success("暂无数据");
        }
        return MessageBean.success(classifies);
    }

}
